﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Com.Maiyoule.GsmSms {
    public class Api {

        private static string GroupName = "default";

        private static List<GsmGroup> group = new List<GsmGroup>();

        /// <summary>
        /// 创建一个分组
        /// </summary>
        /// <param name="groupName"></param>
        /// <param name="setting"></param>
        /// <returns></returns>
        public static GsmGroup CreateGroup(string groupName, Setting setting) {
            GsmGroup gg = FindGroup(groupName);
            if (gg != null) {
                return gg;
            }
            gg = new GsmGroup();
            gg.Name = groupName;
            GsmContral gc = new GsmContral(setting,groupName);
            gg.Contral = gc;
            group.Add(gg);
            gg.Index = group.Count - 1;
            return gg;

        }

        /// <summary>
        /// 移除一个分组
        /// </summary>
        /// <param name="groupName"></param>
        /// <returns></returns>
        public static bool RemoveGroup(string groupName) {
            GsmGroup gg = FindGroup(groupName);
            if (gg == null) {
                return true;
            }
            gg.DisposeAll();
            group.RemoveAt(gg.Index);
            return true;
        }

        /// <summary>
        /// 查找分组
        /// </summary>
        /// <param name="groupName"></param>
        /// <returns></returns>
        public static GsmGroup FindGroup(string groupName) {
            return
            group.Find(delegate(GsmGroup gg) { return gg.Name == groupName; });
        }

        /// <summary>
        /// 设置回调
        /// </summary>
        /// <param name="callback">回调函数</param>
        public static void SetCallBack(IEvent callback) {
            SetCallBack(callback, GroupName);
        }
        /// <summary>
        /// 设置回调
        /// </summary>
        /// <param name="callback">回调函数</param>
        /// <param name="groupName">分组名称</param>
        public static void SetCallBack(IEvent callback,string groupName) {
            GsmGroup gg = FindGroup(groupName);
            if(gg==null){
                return;
            }
            gg.Callback = callback;
        }


        /// <summary>
        /// 打开短信猫
        /// </summary>
        /// <param name="com">端口号名称</param>
        /// <param name="rate">波特率</param>
        public static void Open(string com,int rate) {
            Open(com, rate, GroupName);
        }
        /// <summary>
        ///  打开短信猫
        /// </summary>
        /// <param name="com">端口号名称</param>
        /// <param name="rate">波特率</param>
        /// <param name="groupName">分组名称</param>
        public static void Open(string com, int rate, string groupName) {
            GsmGroup gg = FindGroup(groupName);
            if (gg == null) {
                //创建一个分组
                Setting set = new Setting();
                set.PortName = com;
                set.Rate = rate;
                gg = CreateGroup(groupName, set);
                if(gg==null){
                    return;
                }
            }
            gg.Open();
        }

        public static void Close() {
            Close(GroupName);
        }
        public static void Close(string groupName) {
            GsmGroup gg = FindGroup(groupName);
            if(gg==null){
                return;
            }
            

        }



    }
}
